from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QPushButton, QHBoxLayout
from PyQt5.QtCore import pyqtSignal, QRect
from PyQt5.QtWidgets import (
    QApplication, QWidget, QVBoxLayout, QPushButton,
    QFileDialog, QTableWidget, QTableWidgetItem, QMessageBox, QLineEdit
)
from PyQt5.QtWidgets import (
    QApplication,
    QLabel,
    QMainWindow,
    QPushButton,
    QVBoxLayout,
    QWidget,
)
import pandas as pd
from qt.signal_class import *
from module.log import log_exception
from module.excel_trans import *
from qt.windows import *

class OfflineWindow(QWidget):
    logout_signal = pyqtSignal()

    def __init__(self, db_path):
        super().__init__()
        self.db_path = db_path
        self.width = 1280
        self.height = 720

        self.setWindowTitle("离线客户端")
        self.setGeometry(0, 0, self.width, self.height)

        self.layout = QVBoxLayout()

        self.table = QTableWidget()
        self.table.setDragEnabled(True)
        self.layout.addWidget(self.table)

        self.project_button = QPushButton('新增项目')
        self.project_button.clicked.connect(self.project)
        self.layout.addWidget(self.project_button)

        self.insert_button = QPushButton('增')
        self.insert_button.clicked.connect(self.insert)
        self.layout.addWidget(self.insert_button)

        self.delete_button = QPushButton('删')
        self.delete_button.clicked.connect(self.delete)
        self.layout.addWidget(self.delete_button)

        self.upgrade_button = QPushButton('改')
        self.upgrade_button.clicked.connect(self.upgrade)
        self.layout.addWidget(self.upgrade_button)

        self.search_button = QPushButton('查')
        self.search_button.clicked.connect(self.search)
        self.layout.addWidget(self.search_button)

        self.logout_button = QPushButton("退出登录")
        self.logout_button.clicked.connect(self.logout_signal.emit)
        self.layout.addWidget(self.logout_button)

        self.setLayout(self.layout)

        # 创建信号管理器
        self.signal_manager = SignalManager()

        # 连接信号
        self.signal_manager.show_in_table_signal.connect(self.show_in_table)

        self.project_window = ProjectWindow(self.signal_manager, db_path=self.db_path)
        self.insert_window = InsertWindow(self.signal_manager, db_path=self.db_path)
        self.search_window = SearchWindow(self.signal_manager, db_path=self.db_path)
        self.delete_window = DeleteWindow(self.signal_manager, db_path=self.db_path)
        self.upgrade_window = UpgradeWindow(self.signal_manager, db_path=self.db_path)


    def project(self):
        if self.project_window.isVisible():
            self.project_window.hide()
        else:
            self.project_window.show()

    def insert(self):
        if self.insert_window.isVisible():
            self.insert_window.hide()
        else:
            self.insert_window.show()

    def search(self):
        if self.search_window.isVisible():
            self.search_window.hide()
        else:
            self.search_window.show()

    def delete(self):
        if self.delete_window.isVisible():
            self.delete_window.hide()
        else:
            self.delete_window.show()

    def upgrade(self):
        if self.upgrade_window.isVisible():
            self.upgrade_window.hide()
        else:
            self.upgrade_window.show()

    def show_in_table(self, df):
        """
        通过信号与槽机制，传入dataframe，在table中显示出来
        :return: None
        """
        self.table.setRowCount(df.shape[0])
        self.table.setColumnCount(df.shape[1])
        self.table.setHorizontalHeaderLabels(df.columns.tolist())

        for i in range(df.shape[0]):
            for j in range(df.shape[1]):
                self.table.setItem(
                    i, j, QTableWidgetItem(str(df.iat[i, j]))
                )

    def get_table_df(self):
        """
        通过信号与槽机制，传递table中的dataframe到其他窗口中（太麻烦了，暂且不实现）
        :return: dataframe
        """
        pass


